home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / RKM_Companion_v2.04 / Exec_Library / Ports / port2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-27  |  4.5 KB  |  106 lines

  1. ;/* port2.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 port2.c
  3. Blink FROM LIB:c.o,port2.o TO port2 LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5.  
  6. port2.c - port and message example, run at the same time as port1.c
  7.  
  8. Copyright (c) 1992-1999 Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33. #include <exec/types.h>
  34. #include <exec/ports.h>
  35. #include <exec/memory.h>
  36. #include <dos/dos.h>
  37. #include <clib/exec_protos.h>
  38. #include <clib/alib_protos.h>
  39. #include <stdio.h>
  40.  
  41. #ifdef LATTICE
  42. int CXBRK(void) { return(0); }  /* Disable Lattice CTRL-C handling */
  43. int chkabort(void) {return(0);}
  44. #endif
  45.  
  46. BOOL SafePutToPort(struct Message *, STRPTR);
  47.  
  48. struct XYMessage {
  49.     struct Message xy_Msg;
  50.     WORD           xy_X;
  51.     WORD           xy_Y;
  52. };
  53.  
  54. void main(int argc, char **argv)
  55. {
  56.     struct MsgPort *xyreplyport;
  57.     struct XYMessage *xymsg, *reply;
  58.                                                            /* Using CreatePort() with no name       */
  59.     if (xyreplyport = CreatePort(0,0))                     /* because this port need not be public. */
  60.     {
  61.         if (xymsg = (struct XYMessage *) AllocMem(sizeof(struct XYMessage), MEMF_PUBLIC | MEMF_CLEAR))
  62.         {
  63.             xymsg->xy_Msg.mn_Node.ln_Type = NT_MESSAGE;                /* make up a message,        */
  64.             xymsg->xy_Msg.mn_Length = sizeof(struct XYMessage);        /* including the reply port. */
  65.             xymsg->xy_Msg.mn_ReplyPort = xyreplyport;
  66.             xymsg->xy_X = 10;                                   /* our special message information. */
  67.             xymsg->xy_Y = 20;
  68.  
  69.             printf("Sending to port1: x = %d y = %d\n", xymsg->xy_X, xymsg->xy_Y);
  70.                                                                    /* port2 will simply try to put  */
  71.  
  72.             if (SafePutToPort((struct Message *)xymsg, "xyport"))  /* one message to port1 wait for */
  73.             {                                                      /*  the reply, and then exit     */
  74.                 WaitPort(xyreplyport);
  75.                 if (reply = (struct XYMessage *)GetMsg(xyreplyport))
  76.                     printf("Reply contains: x = %d y = %d\n",         /* We don't ReplyMsg since   */
  77.                             xymsg->xy_X, xymsg->xy_Y);                /* WE initiated the message. */
  78.  
  79.                       /* Since we only use this private port for receiving replies, and we sent     */
  80.                       /* only one and got one reply there is no need to cleanup. For a public port, */
  81.                       /* or if you pass a pointer to the port to another process, it is a very good */
  82.                       /* habit to always handle all messages at the port before you delete it.      */
  83.             }
  84.             else printf("Can't find 'xyport'; start port1 in a separate shell\n");
  85.             FreeMem(xymsg, sizeof(struct XYMessage));
  86.         }
  87.         else printf("Couldn't get memory\n");
  88.         DeletePort(xyreplyport);
  89.     }
  90.     else printf("Couldn't create xyreplyport\n");
  91. }
  92.  
  93.  
  94. BOOL SafePutToPort(struct Message *message, STRPTR portname)
  95. {
  96.     struct MsgPort *port;
  97.  
  98.     Forbid();
  99.     port = FindPort(portname);
  100.     if (port) PutMsg(port, message);
  101.     Permit();
  102.     return(port ? TRUE : FALSE); /* FALSE if the port was not found */
  103.  
  104.          /* Once we've done a Permit(), the port might go away and leave us with an invalid port    */
  105. }        /* address. So we return just a BOOL to indicate whether the message has been sent or not. */
  106.